<template>
  <div class="admin-body">
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-row style="padding: 15px">
      <el-row class="main">
        <!--顶部-->
        <el-row  style="min-height: 40px;display: flex;">
          <el-col  style="margin-bottom: 5px;" align="left">
            <el-input v-model="key" clearable style="width: 300px;" placeholder="输入关键词查询" class="kz_search">
              <el-button slot="append" type="primary"  @click="getStart()">搜索</el-button>
            </el-input>
          </el-col>
          <el-col  style="margin-bottom: 5px;" align="right">
            <el-button type="primary" @click="addShow()" style="">添加</el-button>
          </el-col>
        </el-row>
        <!--表格-->
        <el-table
          border
          @selection-change="handleSelectionChange"
          align="left"
          :data="tableData"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <!--<el-table-column-->
          <!--prop="id"-->
          <!--label="id">-->
          <!--</el-table-column>-->
          <el-table-column
            prop="admin_name"
            label="姓名">
          </el-table-column>
          <el-table-column
            prop="account"
            label="登录账号">
          </el-table-column>
          <el-table-column
            prop="role_id"
            label="类型">
            <template slot-scope="scope">
              {{roleType(scope.row.role_id)}}
            </template>
          </el-table-column>
          <el-table-column
            label="性别">
            <template slot-scope="scope">
              {{ scope.row.sex==1?'男':'女' }}
            </template>
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话">
          </el-table-column>
          <el-table-column
            prop="add_time"
            label="登录时间">
            <template slot-scope="scope">
              {{ formatDate(scope.row.add_time) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="state"
            label="状态">
            <template slot-scope="scope">
              {{ scope.row.state===1?'正常': '禁用'}}
            </template>
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button @click="changeMes(scope.row.id,0)" style="color: red"  size="small" type="text">删除</el-button>
              <el-button @click="editShow(scope.row)"  style="" size="small" type="text">
                编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <el-row style="margin-top: 20px">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" align="right">
            <el-pagination
              style="text-align: right;margin-bottom:70px;"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="dataTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </el-row>
    </el-row>
    <!--新增-->
    <el-dialog title="新增" :visible.sync="addVisible" width="360px" :modal-append-to-body="false">
      <el-form :model="addData" :rules="addRules" ref="addData" style="text-align: left">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="admin_name">
          <el-input clearable v-model="addData.admin_name" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="账号" :label-width="formLabelWidth" prop="account">
          <el-input clearable v-model="addData.account" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="pwd">
          <el-input clearable v-model="addData.pwd" type="password" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
          <el-input clearable v-model="addData.phone" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-select v-model="addData.sex" placeholder="请选择" clearable>
            <el-option
              v-for="item in GenderData"
              :key="item.Gender_Id"
              :label="item.Gender_Name"
              :value="item.Gender_Id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth" prop="role_id">
          <el-select v-model="addData.role_id" placeholder="请选择" clearable>
            <el-option
              v-for="item in qxData"
              :key="item.id"
              :label="item.role_name"
              :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAdd('addData')">确 定</el-button>
      </div>
    </el-dialog>

    <!--编辑-->
    <el-dialog title="编辑" :visible.sync="editVisible" width="360px" :modal-append-to-body="false">
      <el-form :model="editData" :rules="editRules" ref="editData" style="text-align: left">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="admin_name">
          <el-input clearable v-model="editData.admin_name" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="pwd">
          <el-input clearable v-model="editData.pwd" type="password" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
          <el-input clearable v-model="editData.phone" auto-complete="off" placeholder="请输入"
                    required></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-select v-model="editData.sex" placeholder="请选择">
            <el-option
              v-for="item in GenderData"
              :key="item.Gender_Id"
              :label="item.Gender_Name"
              :value="item.Gender_Id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth" prop="role_id">
          <el-select v-model="editData.role_id" placeholder="请选择">
            <el-option
              v-for="item in qxData"
              :key="item.id"
              :label="item.role_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit('editData')">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

  export default {
    name: "Role",
    activated() {
      if (!this.$store.state.login.in) {
        this.$router.push({name: "login", params: {userId: 123}})
      }
      this.getStart()
      this.getqx()

    },
    data() {
      return {
        page: 1,
        phone:[],
        contentVisible:false,
        contentValue:'',
        serchValue:'',
        addSelectValue:0,
        tableData: [],
        pageSize: 10,
        currentPage: 1,
        key:'',
        selectkey: '',
        dataTotal: 0,
        multipleSelection: [],
        formLabelWidth: '80px',
        addVisible: false,
        GenderData:[
          {
            Gender_Id : 1,
            Gender_Name: '男'
          },
          {
            Gender_Id : 2,
            Gender_Name: '女'
          },
        ],
        roleData:[
          //   {
          //     role_Id : 1,
          //     role_Name: '员工'
          //   },
          //   {
          //     role_Id : 2,
          //     role_Name: '高层'
          //   },
          //   {
          //     role_Id : 3,
          //     role_Name: '管理'
          //   },
        ],
        addData: {
          admin_name:'',
          sex:'',
          pwd:'',
          phone:'',
          role_id:'',
          account:'',

        },
        addRules: {
          admin_name: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          sex: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          pwd: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          role_id: [
            {required: true, message: '请输入', trigger: 'blur'}

          ],
          account: [
            {required: true, message: '请输入', trigger: 'blur'}

          ],

        },
        editVisible: false,
        editData: {
          admin_name:'',
          sex:'',
          pwd:'',
          phone:'',
          role_id:'',
          account:'',
        },
        editRules: {
          account: [
            {required: true, message: '请输入', trigger: 'blur'}

          ],
          admin_name: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          sex: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],

          phone: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          role_id: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
        },
        qxData:[],
      }
    },

    methods: {
      // 获取数据
      getStart() {
        let thi = this
        const por = {
          page:thi.page,
          pageSize:thi.pageSize,
          key:thi.key,
        }
        thi.mySelectAll('adminList', por).then(function (value) {
          thi.tableData = value.data
          thi.dataTotal = value.Total
        })
      },
      //权限转换
      roleType(data){
        for(let i=0;i<this.qxData.length;i++){
          if(this.qxData[i].id==data){
            return this.qxData[i].role_name
          }
        }
      },
      // 显示新增
      addShow() {
        this.addVisible = true
        this.addData = {}
        this.addSelectValue=0
      },

      //获取权限
      getqx() {
        let thi = this
        const por = {
          page: thi.page,
          pageSize: 9999,
          key: thi.key
        }
        thi.mySelectAll('roleList', por).then(function (value) {
          thi.qxData = value.data
          // console.log('qData',thi.qxData)
        })
      },
      //提交新增
      submitAdd(formName) {
        let thi = this
        console.log(thi.addData)
        thi.$refs[formName].validate((valid) => {
          if (valid) {
            thi.mySubmit('addAdmin', thi.addData).then(function (code) {
              thi.addVisible = false
              thi.getStart()
            })
          } else {
            return false;
          }
        });
      },
      editShow(row) {
        console.log(row)
        this.editData = JSON.parse(JSON.stringify(row)) //获取
        this.editVisible = true
      },

      //提交编辑
      submitEdit(formName) {
        let thi = this
        thi.$refs[formName].validate((valid) => {
          if (valid) {
            thi.editData.id=thi.editData.id
            thi.mySubmit('editAdmin', thi.editData).then(function (code) {
              if (code == 200) {
                thi.editVisible = false
                thi.getStart()
              }
            })
          } else {
            return false;
          }
        });
      },


      formatDate (date) {
        if(!date){
          return "无"
        }
        Date.prototype.toLocaleString = function() {
          return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate()+ " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
        };
        var unixTimestamp = new Date(date * 1000)
        return unixTimestamp.toLocaleString();
      },


      //多选
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //状态改变提示
      changeMes(id, state) {
        var heeh = '该'
        var caozuo = '禁用'
        if (id.length > 1) {
          heeh = '这些'
        }
        if (state == 0) {
          caozuo = '删除'
        }
        let thi = this
        thi.$confirm('此操作将'+caozuo + heeh+'信息', '是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          thi.submitState(id, state)
        }).catch(() => {
          thi.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      //状态改变
      submitState(id, state) {
        let thi = this
        const por = {
          id: id,
          state: state
        }
        thi.mySubmit('editState', por).then(function (code) {
          if (code == 200) {
            thi.getStart()
          }
        })
      },
      //选择展示数量
      handleSizeChange(val) {
        this.pageSize = val
        this.getStart()
      },
      //跳转页面
      handleCurrentChange(val) {
        this.currentPage = val
        this.getStart()
      },
    }
  }
</script>

<style scoped>
  .admin-body {
    width: 100%;
  }
  .main {
    background-color: white;
    min-height: 500px;
    padding: 10px;
    border: #EDEDED 1px solid;
    border-radius: 5px;
  }
</style>
